package com.check.utils;

import javafx.scene.control.Alert;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;

/**
 * @description:
 * @Author: zyt
 */
public class LogUtils {
    private static String path=System.getProperties().getProperty("user.dir")+"/log";
    public static void appendLog(String log){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String fileName = sdf.format(System.currentTimeMillis());
        fileName = path+"/"+fileName+".log";
        File file = new File(fileName);
        if(!file.exists()){
            file.getParentFile().mkdirs();
            try {
                file.createNewFile();
            } catch (IOException e) {
                Alert alert = new Alert(Alert.AlertType.ERROR);
                alert.setTitle("错误8989");
                alert.setContentText(e.getMessage());
                alert.showAndWait();
                return;
            }
        }
        try(OutputStreamWriter out = new OutputStreamWriter( new FileOutputStream(fileName,true),"utf-8")){
            final String separator = System.lineSeparator();
            sdf = new SimpleDateFormat("yyyy-MM-dd hh-mm-ss");
            String time = sdf.format(System.currentTimeMillis());
            out.append(time+":"+log+separator);
            out.flush();
        }catch (Exception e){
            e.printStackTrace();
            Alert alert = new Alert(Alert.AlertType.ERROR);
            alert.setTitle("错误8888");
            alert.setContentText(e.getMessage());
            alert.showAndWait();
        }
    }
}
